<?php

namespace App\Logic\Admin;

use App\Constants\ErrorCode;
use App\Service\Admin\LoginService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class AuthLogic
{
    #[Inject]
    protected LoginService $loginService;
    //使用自定义验证器
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;
    /**
     * Notes:后台登录
     * 2025/5/29
     * @param array $params 前端传参
     * @return array
     * @throws \Exception
     */
    public function login(array $params):array
    {
        $validator = $this->validationFactory->make(
            $params,
            [
                'username' => 'required',
                "password" => 'required',
            ],
            [
                'username.required' => ':attribute 参数必传',
                'password.required' => ':attribute 参数必传',
            ]
        );
        if ($validator->fails()){
            throw new \Exception($validator->errors()->first(), ErrorCode::PARAM_ERROR);
        }
        return $this->loginService->login($params['username'],$params['password'],$params['ip']);
    }
}